package com.kindness.frate.pull;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.kindnesslibrary.util.EnableLinearManager;
import com.kindnesslibrary.util.WidgetUtil;

/**
 * Created by Administrator on 2017/7/25 0025.
 */
public class ScrollRecyclerView extends RecyclerView {

    private float oldY;
    private float scrollY;
    private float mDownPosX = 0;
    private float mDownPosY = 0;
    PullRefreshRecyclerView pullRefreshRecyclerView;
    EnableLinearManager enableLinearManager;

    public ScrollRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if(pullRefreshRecyclerView == null)
        if(getParent() instanceof PullRefreshRecyclerView){
            pullRefreshRecyclerView = (PullRefreshRecyclerView) getParent();
            if(getLayoutManager() instanceof EnableLinearManager){
                enableLinearManager = (EnableLinearManager) getLayoutManager();
            }
        }
        switch (e.getAction()){
            case MotionEvent.ACTION_MOVE:{
                if(oldY == 0){
                    oldY = e.getY();
                }
                float Y = e.getY();
                float Ys = Y - oldY;
                //滑动到顶部让父控件重新获得触摸事件
                scrollY = WidgetUtil.getScollYDistance(this);
                if (Ys > 0 && scrollY == 0) {
                    if(pullRefreshRecyclerView != null) {
                        pullRefreshRecyclerView.onTouchEvent(e);
                        enableLinearManager.setScrollEnabled(false);
                    }
                }else{
                    if(enableLinearManager != null){
                        enableLinearManager.setScrollEnabled(true);
                    }
                }
                if(pullRefreshRecyclerView != null) {
                    if (pullRefreshRecyclerView.getPullState() != PullRefreshRecyclerView.DONE) {
                        enableLinearManager.setScrollEnabled(false);
                        pullRefreshRecyclerView.onTouchEvent(e);
                    }
                }
                getParent().requestDisallowInterceptTouchEvent(true);
            }
            break;
            case MotionEvent.ACTION_DOWN:{
                oldY = e.getY();
            }
            break;
            case MotionEvent.ACTION_UP:{
                ((View)getParent()).onTouchEvent(e);
                getParent().requestDisallowInterceptTouchEvent(false);
                oldY = 0;
            }
            break;
        }
        return super.onTouchEvent(e);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        final float x = e.getX();
        final float y = e.getY();
        final int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownPosX = x;
                mDownPosY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                final float deltaX = Math.abs(x - mDownPosX);
                final float deltaY = Math.abs(y - mDownPosY);
                // 这里是够拦截的判断依据是左右滑动，读者可根据自己的逻辑进行是否拦截
                if (deltaX < deltaY) {
                    return true;
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}
